/**
 * @param {number[][]} intervals
 * @return {number[][]}
 */
var merge = function (intervals) {
  if (intervals.length <= 1) return intervals;
  let resultArr = [];
  intervals.sort((a, b) => a[0] - b[0]);
  resultArr.push(intervals[0]);
  for (let i = 1; i < intervals.length; i++) {
    if (intervals[i][0] > resultArr[resultArr.length - 1][1]) {
      resultArr.push(intervals[i]);
    } else {
      resultArr[resultArr.length - 1][1] = Math.max(
        intervals[i][1],
        resultArr[resultArr.length - 1][1]
      );
    }
  }
  return resultArr;
};

let arr = [
  [1, 2],
  [0, 2],
  [4, 5],
  [3, 5],
];
merge(arr).forEach((item) => console.log(item));
// console.log(merge(arr));
